import { Component, OnInit, Output, Input, EventEmitter, ViewChild } from '@angular/core';
import { PagesService } from 'services/pages.service';
declare var $: any;

@Component({
  selector: 'app-cp-select',
  templateUrl: './cp-select.component.html',
  styleUrls: ['./cp-select.component.scss']
})
export class CpSelectComponent implements OnInit {

  constructor(public interfaceService: PagesService) { }

  @Output() private outer = new EventEmitter();
  @Input() module: any;
  @Input() data: any;
  @Input() name: any;
  @Input() title: any;
  @Input() class: any;
  @Input() css: any;
  @Input() iid: any;
  @Input() disabled: any;
  @Input() listName: any;
  @Input() optionListName: any;
  @Input() optionName: any;
  @Input() optionValue: any;
  @Input() type: any;
  @Input() num: any;

  style = {};
  funNum = '';
  ngOnInit() {
    this.title = this.title === undefined ? '' : this.title;
    this.disabled = this.disabled === undefined ? this.data + '_dis' : this.disabled;
    this.listName = this.listName === undefined ? this.data + '_sel' : this.listName;
    this.optionListName = this.optionListName === undefined ? this.name : this.listName;
    this.optionName = this.optionName === undefined ? 'optionName' : this.optionName;
    this.optionValue = this.optionValue === undefined ? 'optionValue' : this.optionValue;
    if (this.type === 'search') {
      this.module[this.data][this.name + 'Cp'] = '';
    } else if (this.type === 'function') {
      if (this.num) {
        this.funNum = this.num;
      }
    }
    /* else if (this.type === 'print') {
     this.interfaceService.printBut(this.module);
   } */
    this.ngCss();
  }

  ngCss() {
    if (this.css) {
      var cssList = this.css.split(";");
      for (var i = 0; i < cssList.length; i++) {
        var cssParm = cssList[i].split(":");
        this.style[cssParm[0]] = cssParm[1];
      }
    }
  }


  searchHid = false;
  inputSearchClick() {
    this.searchHid = true;
  }
  selectSearchClick() {
    const selectValue = this.module[this.data][this.name];
    const selectList = this.module[this.listName][this.optionListName];
    for (var i = 0; i < selectList.length; i++) {
      if (selectList[i][this.optionValue] == selectValue) {
        this.module[this.data][this.name + 'Cp'] = selectList[i][this.optionName]
      }
    }
    this.searchHid = false;
    this.outer.emit(selectValue);
  }

  inputClick() {
    this.module[this.data + '_hid'][this.name] = true;
  }
  selectClick() {
    const selectValue = this.module[this.data][this.name];
    const selectList = this.module[this.listName][this.optionListName];
    for (var i = 0; i < selectList.length; i++) {
      if (selectList[i][this.optionValue] == selectValue) {
        this.module[this.data][this.name + 'Cp'] = selectList[i][this.optionName]
      }
    }
    this.module[this.data + '_hid'][this.name] = false;
    if (this.module[this.data][this.name]) {
      this.outer.emit(selectValue);
    }
  }

  compareFn = (o1: any, o2: any) => (o1 && o2 ? o1.operationName === o2.operationName : o1 === o2);
  isCourse_gn = true;
  inputTypeClick() {
    this.isCourse_gn = false;
  }

  selectTypeClick() {
    this.isCourse_gn = true;
    if (this.module['functionName'+this.funNum]) {
      if (this.outer.observers.length > 0) {
        this.module['functionName'+this.funNum+'Cp']=this.module['functionName'+this.funNum]['operationName']
        this.outer.emit(this.module['functionName'+this.funNum]);
      } else {
        if (this.num) {
          this.interfaceService.functionSub(this.module, this.module['functionName'+this.funNum], this.module[this.data]);
        }else{
          this.interfaceService.functionSub(this.module, this.module['functionName'+this.funNum], this.module[this.data],this.num);
        }
      }
    }
  }

  printName = null;
  printChange(e) {
    this.interfaceService.printSub(this.module, e);
    this.printName = null;
    this.isCourse_gn = true;
  }

  inputAttClick(name) {
    for (var x = 0; x < this.module[this.data + '_att'].length; x++) {
      if (this.module[this.data + '_att'][x]['propId'] == name) {
        this.module[this.data + '_att'][x][name] = true;
        setTimeout(() => {
          $('#' + this.random+name + ' input').click();
        }, 10);
        break;
      }
    }
  }

  selectAttClick(name, value, list) {
    for (var i = 0; i < list.length; i++) {
      if (list[i]['tableFlie'] == value) {
        for (var x = 0; x < this.module[this.data + '_att'].length; x++) {
          if (this.module[this.data + '_att'][x]['propId'] == name) {
            this.module[this.data + '_att'][x]['defaultValueCp'] = list[i]['fileName'];
            this.module[this.data + '_att'][x][name] = false;
            break;
          }
        }
        break;
      }
    }
  }

  DateClick(e, name, type) {
    for (var x = 0; x < this.module[this.data + '_att'].length; x++) {
      if (this.module[this.data + '_att'][x]['propId'] == name) {
        this.module[this.data + '_att'][x]['defaultValue'] = this.GMTToStr(e, type);
        this.module[this.data + '_att'][x][name] = true;
        break;
      }
    }
  }

  DateOpen(e,name) {
    if(!e){
      for (var x = 0; x < this.module[this.data + '_att'].length; x++) {
        if (this.module[this.data + '_att'][x]['propId'] == name) {
          this.module[this.data + '_att'][x][name] = false;
          break;
        }
      }
    }
  }

  GMTToStr(time, type) {
    let date = new Date(time);
    let Str;
    if (type === 'year') {
      Str = date.getFullYear();
    } else if (type === 'month') {
      Str = date.getFullYear() + '-' +
        (date.getMonth() + 1);
    } else if (type === 'datetime') {
      Str = date.getFullYear() + '-' +
        (date.getMonth() + 1) + '-' +
        date.getDate() + ' ' +
        date.getHours() + ':' +
        date.getMinutes() + ':' +
        date.getSeconds();
    } else {
      Str = date.getFullYear() + '-' +
        (date.getMonth() + 1) + '-' +
        date.getDate() + ' ';
    }
    return Str;
  }

  //报表自定义弹窗
  @ViewChild('popupChild') popup: any;
  popup9_show(crpId): void {
    this.popup.popup11_show(crpId, this.module['switchUnitId']);
  }
  popupParent(e) {
    for (let i in e) {
      for (var x = 0; x < this.module[this.data + '_att'].length; x++) {
        if (this.module[this.data + '_att'][x]['propId'] == i) {
          this.module[this.data + '_att'][x]['defaultValue'] = e[i];
        }
      }
    }
  }
  random = new Date().getTime() + Math.random().toString(36).substr(2, 10);

  resetData() {
    if (!this.type) {
      this.module[this.data][this.name] = '';
      this.module[this.data][this.name + 'Cp'] = '';
      this.selectClick();
    } else if (this.type === 'search') {
      this.module[this.data][this.name] = '';
      this.module[this.data][this.name + 'Cp'] = '';
      this.selectSearchClick();
    } else if (this.type === 'function') {
      this.module['functionName'+this.funNum+'Cp']='';
      this.module['functionName'+this.funNum]=null;
    } else if (this.type === 'print' && this.module['printList'].length > 0) {

    } else if (this.type === 'att') {

    }
  }



  /* inputAttClick(name) {
    this.module[this.data + '_hid'][name] = false;
  }
  selectAttClick(name, list) {
    const selectValue = this.module[name];
    for (var i = 0; i < list.length; i++) {
      if (list[i]['code'] == selectValue) {
        this.module[name + 'Cp'] = list[i]['description'];
      }
    }
    this.module[this.data + '_hid'][name] = true
    this.outer.emit(selectValue);
  } */


}
